/**
 * @author Travis C lynch <travisclynch@student.midlandstech.edu>
 * @version May 06 2012
 * <p>
 * <b>Instructions:</b>
 * <p>
 * Write a program using text file I/O to open any file and print out all of the integers in the file, and their sum. 
 * You should input one character at a time, and convert contiguous characters into integers.
 * <ul>
 * <li>Prompt for a name and open the file.
 * <li>Scan the file, and loop through each character, ignoring any character that is not a digit or a -sign.
 * <ul>
 * <li>If it is a - sign, store that information so the integer will be negative.
 * <li>If it is a digit, set a currentInt value to the numeric value of that digit, and loop as long as there are more digits, multiplying currentInt by 10 and adding the numeric value of the next digit.
 * <li>After the loop exits, multiply by -1 if it is negative, print it out and add it to a total.
 * </ul>
 * <li>Be careful that you don't try to read past the end of the file.
 * </ul>
 * Upload well-documented Java source code, documented in the JavaDoc style so that the javadoc program would produce good output if it were run on it, to this dropbox.
 * <p>
 * <b>Description:</b><br>
 * A method to import data from a file
 */
import java.io.*;
import java.util.*;
public class HW5 {
	public static void main(String[] args) {
		/** File name for the I/O to read; Buffer variable to hold read data for processing */
		String fname, buffer = null;
		/** Array to hold the values read inside txt doc */
		int[] intagers;
		/** Int that holds the number of integers processed */
		int numberOfIntagers = 0;
		BufferedReader istream = null;
		boolean isNegative = false;
		Scanner kbd = new Scanner(System.in);
		System.out.print("Enter a filename with full location (use forward slashes insted of backslashes): ");
		fname = kbd.next();
		try{
			istream = new BufferedReader(new FileReader(fname));
		}
		catch (IOException e){
			System.err.println(e.getMessage());
		}
		Scanner file = new Scanner(istream);
		while(file.hasNext()){
			buffer += file.nextLine();
		}
		intagers = new int[buffer.length()];
		for(int i=0; i<buffer.length(); i++){
			char ch = buffer.charAt(i);
			if (ch == '-'){
				isNegative = true;
			}else if(Character.isDigit(ch)){
				while(Character.isDigit(ch) && (i+1)<buffer.length()){
					intagers[numberOfIntagers]*=10;
					intagers[numberOfIntagers]+=Character.getNumericValue(ch);
					i++;
					ch = buffer.charAt(i);
				}
				if(isNegative){
					intagers[numberOfIntagers]*=(-1);
				}
				numberOfIntagers++;
			}else{
				isNegative = false;
			}
		}
		System.out.println(intagers.toString());
		System.out.print("Total: " + addArray(intagers));
		file.close();
	}
	public static int addArray(int[] values) {  
		int total=0;
		for(int i=0; i<values.length; i++){
			total += values[i];
		}
		return total;
	}

}
